<?php
include_once '../tiles/header.php';
include_once 'I_DAO.class.php';

class MessageDAO extends I_DAO {
	
	private $msgId;
	private $msgUsrIdSender;
	private $msgUsrIdReceiver;
	private $msgDate;
	private $msgTitle;
	private $msgContent;
	
	public function setValues($id, $sender, $receiver, $date, $title, $content) {
		$this->msgId = $id;
		$this->msgUsrIdSender = $sender;
		$this->msgUsrIdReceiver = $receiver;
		$this->msgDate = $date;
		$this->msgTitle = $title;
		$this->msgContent = $content;
	}
	
	public function create() {
		/*Sprawdzamy czy wymagane pola zostały wypełnione.
		  Jezeli nie to rzucamy wyjatek
		  TODO: Napisać własną hierarchię wyjątków DAO*/
		if( empty( $this->msgUsrIdSender ) ) {
			throw new Exception('ERROR: msgUsrIdSender cannot be empty!');
			return false;
		}
		if( empty( $this->msgUsrIdReceiver ) ) {
			throw new Exception('ERROR: msgUsrIdReceiver cannot be empty!');
			return false;
		}
		
		/*Pobieramy connection ze zmiennej $_REQUEST 
		  (czy na pewno mozemy zalozyc ze istnieje?)*/
		$conn = $_REQUEST['connection'];

		$statement = $conn->getPreparedStatement(
			'INSERT INTO messages VALUES(NULL, ?, ?, NULL, ?, ?)');
		$statement->bind_param('iiss', 
			$this->msgUsrIdSender,
			$this->msgUsrIdReceiver,
			$this->msgTitle,
			$this->msgContent);	
		$conn->executePreparedStatement($statement);
		$statement->close();
	}
	
	public function alter() {}
	
	/*Funkcja sprawdza, które wartości w DAO są ustawione i na ich podstawie
      wykonuje SELECT w bazie danych.
	  Zwraca kolumny zawarte w tablicy podanej jako parametr*/
	public function fetch($sqlConditions) {
		
		//Zmiana podejscia do metody fetch. 
		//Argument - czesc zapytania sql po słowie WHERE
		//Zwracana - tablica wypełnionych obiektów DAO
		
		$query = 'SELECT * FROM messages ';
		
		//Pusty argument funckji jest dozwolony
		//zwramamy wtedy wszystkie elementy tablicy
		//(czyli nie dodajemy nic do zapytania)
		if ( !empty($sqlConditions) ) {
			$query .= ' WHERE ' . $sqlConditions;
		}
		$query .= ';';
		
		//Debug
		print 'DAOmessages fetch query: ' . $query . '<br/>';
		
		//Pytamy baze
		$db_conn = $_REQUEST['connection'];
		$db_statement = $db_conn->getPreparedStatement($query);
		$result = $db_conn->executePreparedStatement($db_statement);
		
		//Tworzymy, wypelniamy obiekty DAO (które pełnią role entityBeanów),
		//pakujemy je do tablicy i zwracamy.
		print 'Succeed?: ' . $result . '<br/>';
		
		$a; $b; $c; $d; $e; $f;
		$db_statement->bind_result($a,$b,$c,$d,$e,$f);
		$result = Array();
		while( $db_statement->fetch() ) {
			//print $a.' '.$b.' '.$c.' '.$d.' '.$e.' '.$f . '<br/>';
			$bean = new MessageDAO();
			$bean->setValues($a, $b, $c, $d, $e, $f);
			$result[] = $bean;
		}
		
		return $result;
	}
	
	public function count() {}
	public function delete() {}
	
	public function cout($version) {
		print $this->msgId . '|' . $this->msgUsrIdSender . '|' .
			  $this->msgUsrIdReceiver . '|' . $this->msgUsrIdSender . '|' .
			  $this->msgTitle . '|';
		if($version == 'full') {
			print $this->msgContent;
		}
		print '</br>';
	}
	
	/*
		Setters - Getters
	*/
	public function setMsgUsrIdSender($id) {
		$this->msgUsrIdSender = $id;
	}
	public function getMsgUsrIdSender() {
		return $this->msgUsrIdSender;
	}
	public function setMsgUsrIdReceiver($id) {
		$this->msgUsrIdReceiver = $id;
	}
	public function getMsgUsrIdReceiver() {
		return $this->msgUsrIdReceiver;
	}
	public function setMsgTitle($title) {
		$this->msgTitle = $title;
	}
	public function getMsgTitle() {
		return $this->msgTitle;
	}
	public function setMsgContent($content) {
		$this->msgContent = $content;
	}
	public function getMsgContent() {
		return $this->msgContent;
	}
	public function getMsgDate() {
		return $this->msgDate;
	}
	public function getMsgId() {
		return $this->msgId;
	}
} /*Class end*/


//------------------------------------------
//TEST AREA
try {
	
	$DAO = new MessageDAO();
	$DAO->setMsgUsrIdReceiver(11);
	$DAO->setMsgUsrIdSender(234);
	$DAO->setMsgTitle('Tytuł');
	$DAO->setMsgContent('Content');
	$DAO->create();
	
	$DAO2 = new MessageDAO();
	$result = $DAO2->fetch('');
	foreach ($result as $r) {
		$r->cout('full');
	}
	
} catch (Exception $e) {
	echo "Złapałem wyjątek: </br>" . $e;
}

print '<br/><br/>Connection id: ' . $_REQUEST['connection']->getId();
?>